package cn.net.Thread;
/*需求:设计多线程编程模型,4个窗口共计售票100张*/
/*本类通过实现Runnable接口的方式实现多线程售票案例*/
/*
优化方法：增加同步锁，格式：synchronized (类名.class) {}
 */
public class SoleTicketV2 {
    public static void main(String[] args) {
        TicketThread2_2 t = new TicketThread2_2();
        Thread t1   = new Thread(t);
        Thread t2   = new Thread(t);
        Thread t3   = new Thread(t);
        Thread t4   = new Thread(t);
        t1.start();
        t2.start();
        t3.start();
        t4.start();


    }
}
//实现接口+实现重写方法run（）
class TicketThread2_2 implements Runnable{
  //声明静态表示唯一成员变量
    static int tickets = 100;
    @Override
    public void run() {
        while (true){
            try {
                Thread.sleep(10);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }//Thread.currentThread().getName()
            System.out.println(Thread.currentThread().getName()+"="+tickets--);
             if(tickets<=0) break;
        }

    }
}